#include <iostream>
#include "fstream"
#include "string.h"
#include "common.cpp"

using namespace std;


// 员工管理系统结构体
struct EmployeeManage{
    int index=0;   // 指示当前储存到第几位数据
    Employee * employee[1000] = {nullptr};
};

static EmployeeManage em;

// 展示菜单函数
void showMenu() {
    cout << "**************************************" << endl;
    cout << "************欢迎使用员工管理系统*********" << endl;
    cout << "*************1、退出管理系统************" << endl;
    cout << "*************2、增加职工信息************" << endl;
    cout << "*************3、显示职工信息************" << endl;
    cout << "*************4、删除离职职工************" << endl;
    cout << "*************5、修改职工信息************" << endl;
    cout << "*************6、查找职工信息************" << endl;
    cout << "*************7、按照编号排序************" << endl;
    cout << "*************8、清空所有文档************" << endl;
    cout << "**************************************" << endl;
}

// 接入外部输入，并过滤部分错误输入
int inputFunction(){
    cout << "请输入:";
    int inputNum;
    cin >> inputNum;
    if(inputNum>=9||inputNum<=0){
        cout << "输入错误，请重新输入" << endl;
        return -1;
    }
    return inputNum;
}

// 将新增员工信息写入文件,返回错误信息
void writeToFile(Employee * employee){
    // 1、导入头文件
    // 2、创建流文件
    ofstream ofs;
    // 3、打开文件
    ofs.open("employee.data", ios::app);
    // 4、写入数据
    ofs << employee->id;
    ofs << "\t";
    ofs << employee->name;
    ofs << "\t";
    ofs << employee->level;
    ofs << "\n";
    // 5、关闭文件
    ofs.close();
}

// 增加员工信息
void addEmployee(){
    int id;
    string name;
    cout << "请输入职工编号:";
    cin >> id;
    cout << "请输入职工姓名:";
    cin >> name;
    bool isLoop = true;
    int level;
    while (isLoop){
        cout << "请输入职工职位(老板=1、经理=2、雇员=3):";
        cin >> level;
//        cout << name << endl;
//        cout << level << endl;
        if(level == 1) {
            Boss * b = new Boss(id, name);
            em.employee[em.index++] = b;
            isLoop = false;
        }else if(level == 2) {
            Manager * m = new Manager(id, name);
            em.employee[em.index++] = m;
            isLoop = false;
        }else if(level == 3) {
            Staff * s = new Staff(id, name);
            em.employee[em.index++] = s;
            isLoop = false;
        }
        if(level >3||level<1){
            cout << "输入错误，重新输入" << endl;
//            isLoop = false;
        }
    }
    cout << "成功" << endl;
}

// 展示所有员工信息
void showEmployee(){
    if(em.index==0){
        cout << "当前无员工" << endl;
    }else{
        cout << "编号\t\t姓名\t\t职责" << endl;
        for (int i = 0; i < em.index; ++i) {
            cout<<em.employee[i]->id;
            cout<<"\t\t";
            cout<<em.employee[i]->name;
            cout<<"\t\t";
            em.employee[i]->work();
            cout << endl;
        }
    }
}

// 初始化函数，主要功能是每次服务启动时判断文件中是否有信息，若有则读入系统
void init_function(){
    // 1、导入头文件
    // 2、创建流对象
    ifstream ifs;
    // 3、打开文件
    ifs.open("employee.data", ios::in);
    if(!ifs.is_open()){  // 判断文件是否存在
        return;
    }
    // 4、读取数据
    char buf[1024] = {0};
    while(ifs.getline(buf, sizeof(buf))){
        const char *s = "\t";
        char *p;
        p = strtok(buf, s);
        int index = 0;
        int id, level;
        string name;
        while (p){
            if(index==0){
                id = atoi(p);
            }else if(index==1){
                name = p;
            }else if(index==2){
                level = atoi(p);
            }
            index++;
            p = strtok(nullptr, s);
        }
        if(level == 1){
            Boss * b = new Boss(id, name);
            em.employee[em.index++] = b;
        }else if(level == 2){
            Manager * m = new Manager(id, name);
            em.employee[em.index++] = m;
        } else{
            Staff * s = new Staff(id, name);
            em.employee[em.index++] = s;
        }
    }
    // 5、关闭文件
    ifs.close();
}

// 查找员工
int find(int findId){
    for (int i = 0; i < em.index; ++i) {
        if(em.employee[i]->id == findId){
            return i;  // 找到，返回下标
        }
    }
    return -1;  // 未找到
}

// 按照编号排序
void sortEmployee(){
    cout << "输入排序的顺序(1-升序，2-降序):";
    int num;
    cin >> num;
    for (int i = 0; i < em.index; ++i) {
        for (int j = 0; j < em.index-1; ++j) {
            if(num==1 && em.employee[j]->id>em.employee[j+1]->id){
                Employee * temp = em.employee[j+1];
                em.employee[j+1] = em.employee[j];
                em.employee[j] = temp;
            }
            if(num==2 && em.employee[j]->id<em.employee[j+1]->id){
                Employee * temp = em.employee[j+1];
                em.employee[j+1] = em.employee[j];
                em.employee[j] = temp;
            }
        }
    }
}

// 清空所有文档  两次确认
void clearAll(){
    cout << "确认是否要清空所有文档(1-是，2-否):";
    int num;
    cin >> num;
    if(num == 2){
        return;
    }
    for (int i = 0; i < em.index; ++i) {
        em.employee[i] = nullptr;
        em.index--;
    }
    cout << "已清空" << endl;
}

// 修改员工信息
void modifyEmployee(int modifyId){
    int id = find(modifyId);
    if(id == -1){
        cout << "查无此人" << endl;
        return;
    }
    addEmployee();
}

void findEmployee(int findId) {
    int id = find(findId);
    if(id == -1){
        cout << "查无此人" << endl;
        return;
    }
    cout<<em.employee[id]->id;
    cout<<"\t\t";
    cout<<em.employee[id]->name;
    cout<<"\t\t";
    em.employee[id]->work();
    cout << endl;
}

// 删除员工信息
void deleteEmployee(int deleteId){
    int id = find(deleteId);
    if(id == -1){
        cout << "该员工不存在" << endl;
        return;
    }
    if(id==999){
        em.employee[999] = nullptr;
    }else{
        for (int i = id; i < em.index; ++i) {  // 让从删除位置开始的指针每位向前挪一位
            em.employee[i] = em.employee[i+1];
        }
        em.index--;
    }
    cout << "删除成功" << endl;
}

// 关闭程序时做的一系列操作
void clearAndSave() {
    remove("employee.data");
    for (int i = 0; i < em.index; ++i) {
        writeToFile(em.employee[i]);
    }
    for (int i = 0; i < em.index; ++i) {
        delete em.employee[i];   // 释放堆内存指针
    }
}

int main() {
    bool isLoop = true;
    init_function();
    while (isLoop){
        showMenu();
        int inputNum = inputFunction();
        switch (inputNum) {
            case 1:
                clearAndSave();
                isLoop = false;
                break;
            case 2:
                addEmployee();
                break;
            case 3:
                showEmployee();
                break;
            case 4:
                cout << "请输入要删除的员工编号:";
                int deleteId;
                cin >> deleteId;
                deleteEmployee(deleteId);
                break;
            case 5:
                cout << "请输入要修改的员工编号:";
                int modifyId;
                cin >> modifyId;
                modifyEmployee(deleteId);
                break;
            case 6:
                cout << "请输入要查找的员工编号:";
                int findId;
                cin >> findId;
                findEmployee(findId);
                break;
            case 7:
                sortEmployee();
                break;
            case 8:
                clearAll();
                break;

        }
    }

    return 0;
}
